import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import { getGoods, saveGoods, updateGoods, getTotalCount,deleteGoodsById } from '../common/localStorageHelper'

const store = new Vuex.Store({
    state: {
        buyCount: 0
    },
    mutations: {
        //添加商品
        addGoods(state, goods) {
            state.buyCount = saveGoods(goods)
        },
        //修改商品的数量
        changeGoodsNumber(state, changedGoods) {
            state.buyCount = updateGoods(changedGoods)
        },
        //根据id删除对应的商品
        deleteGoodsById(state,goodsId){
            state.buyCount = deleteGoodsById(goodsId)
        }
    },
    getters: {
        //获取购买商品的总数
        getBuyCount(state) {
            if (state.buyCount > 0) {
                return state.buyCount
            } else {
                //第一次
                return getTotalCount()
            }

        }
    }
})

export default store
